﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Tedu.ARPGDemo.Character;

namespace ARPGDemo.Skill
{
    /// <summary>
    /// 伤害
    /// </summary>
    public class DamageImpact : IImpact
    {
        //private SkillData data;
        public void DoImpact(SkillDeployer deployer)
        {
            //data = deployer.CurrentSkillData;
            //通过技能释放器开启协程执行重复攻击
            deployer.StartCoroutine(RepeatAttack(deployer));//脚本才能开启协程
        }
        private IEnumerator RepeatAttack(SkillDeployer deployer)
        {
            float atkTime = 0;
            do
            {
                if (deployer.CurrentSkillData.attackTargets != null)
                {
                    Attack(deployer.CurrentSkillData);
                }
                yield return new WaitForSeconds(deployer.CurrentSkillData.atkInterval);
                atkTime += deployer.CurrentSkillData.atkInterval;
                //再次计算攻击目标
                deployer.CalculateTargets();
            } while (atkTime< deployer.CurrentSkillData.durationTime);
        }
        private void Attack(SkillData data)
        {
            //实际攻击力 = 基础攻击力 * 比率
            float atk = data.owner.GetComponent<CharacterStatus>().baseATK * data.atkRatio;
            //遍历所有攻击目标
            for (int i = 0; i < data.attackTargets.Length; i++)
            {
                data.attackTargets[i].GetComponent<CharacterStatus>().Damage(atk);
            }
        }
    }
}

